_Pragma("once");

#include <string>
#include <regex>
#include <cstdlib>

#include "common.h"
#include "ICurrentVoltageDetector.h"
#include "HalUtility.h"
#include "MachineDependenceConst.h"


/**
 *  交流检测仪
 * 修改相位，查询电压电流数据,U指令，电压电流显示页面
 * 实时获取检测模块的数据
 * @author lizb
 */

class AcDetector : public ICurrentVoltageDetector {
    constexpr static char NO_CURRENT[] = "00.0";
    constexpr static char NO_VOLTAGE[] = "000";
public:

    AcDetector(DummyChannel& channel, const ExtendDeviceType& deviceType, const std::string& deviceActknowlege, const int32_t startIndex = 0, const int32_t portCount = MDConst::DEFAULT_INTERNAL_LOOP_COUNT);

    ~AcDetector();

    bool initialize() noexcept override;


    const std::string& getPhase() override {
        return mloopPhases;
    }


    /**
     * 设置所有相位
     * @param phaseString
    */
    void setPhase(const std::string& phases) override;


    /**
     * 每次修改相位后保存修改到文件
     * @param phase
     * @param loopIndex
     */
    void setPhase(const int loopIndex, const char phase) override{
        mloopPhases[loopIndex] = phase;
        buildCommandAndSavePhase();
    }

    const CurrentVoltageInfo* getCurrentVoltageInfos() override {
        return mcurrentVoltageInfos;
    };

    /**
     * 根据检测模块更新电流电压数据
     */
    bool updateInformation()override;

    bool analysisTrippingOperation(const std::string& loopStatus);

    bool analysisVoltageAlert(void);

protected:
        virtual void buildCommandAndSavePhase();

        void parseVoltageCurrent(const std::string& dataString, CurrentVoltageInfo* pcurrentVoltageInfos);

    /** [序列号/电流值/回路通断]     [001/11.81/1]     */
    static const std::regex pattern;

    CurrentVoltageInfo* mcurrentVoltageInfos = nullptr;

    std::string mloopPhases;
    std::string mcommandBuilder;

    std::string mtrippingAlert;
    bool voltageAlert;
};
